package iss.java;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import iss.java.list.MyList;
import iss.java.list.Node;
import java.util.List;

/**
 * Created by wenke on 2016/9/16.
 */
public class Main_B {
    public static void main(String[] args)throws InterruptedException  {
        // TODO: Write a multithreaded testcase against requirement B.
        // NOTE: Testcase for requirement C is not required.
        MyList list = new MyList();

        Node node = list.getHead();

        Lock lock = new ReentrantLock();

        Thread i1 = new Thread(new InsertThread(list, node, 0, lock));
        Thread i2 = new Thread(new InsertThread(list, node, 1, lock));
        Thread i3 = new Thread(new InsertThread(list, node, 2, lock));
        Thread i4 = new Thread(new InsertThread(list, node, 3, lock));
        Thread i5 = new Thread(new InsertThread(list, node, 4, lock));
        Thread i6 = new Thread(new InsertThread(list, node, 5, lock));


        i1.start();
        i2.start();
        i3.start();
        i4.start();
        i5.start();
        i6.start();

        Thread.sleep(2000);

        Thread r1 = new Thread(new RemoveThread(list, list.getHead().getNext(), lock));
        Thread r2 = new Thread(new RemoveThread(list, list.getHead().getNext().getNext(), lock));
        r1.start();
        r2.start();

        int m = 0 ,n = 0;
        Node node1=list.getHead().getNext();
        Node node2=list.getTail().getPrev();
        for(int i=1;i<100;i++){
            if(node1!=null)
            System.out.println("链表中第" + i + "个数字为"+node1.getData());
            node1=node1.getNext();
            m++;
            if(node1.getNext()==null){
                break;
            }
        }

        for(int i=1;i<100;i++){
            if(node2!=null)
            System.out.println("链表中倒数第" + i + "个数字为"+node2.getData());
            node2=node2.getPrev();
            n++;
            if(node2.getPrev()==null){
                break;
            }
        }
        if(n==list.getSize()&&m==list.getSize())
            System.out.println("正向遍历及反向遍历均无误！ReentrantLock 互斥锁正常使用");

        MyList list1= new MyList();
        MyList list2= new MyList();

        list1.insert(list1.getHead(), 0, lock);
        list1.insert(list1.getHead(), 1, lock);
        list1.insert(list1.getHead(), 2, lock);
        list1.insert(list1.getHead(), 3, lock);

        list2.insert(list2.getHead(), 0, lock);
        list2.insert(list2.getHead(), 1, lock);
        list2.insert(list2.getHead(), 2, lock);
        list2.insert(list2.getHead(), 3, lock);


    }
}
class InsertThread implements Runnable{
    private int number;
    private Node node;
    private MyList list;
    private Lock lock = new ReentrantLock();

    InsertThread(MyList list, Node node, int num,Lock lock) {
        this.number = num;
        this.node = node;
        this.list = list;
        this.lock = lock;
    }

    public void run() {
        list.insert(node, number,lock);
    }
}

class RemoveThread implements Runnable{
    private Node node;
    private MyList list;
    private Lock lock = new ReentrantLock();

    RemoveThread(MyList list,Node node,Lock lock){
        this.list = list;
        this.lock = lock;
        this.node = node;
    }

    public void run() {
        list.remove(list,node,lock);
    }
}

